//
//  ViewController.m
//  HUD
//
//  Created by Justsoso on 16/3/28.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"
#import <MBProgressHUD.h>

@interface ViewController ()<MBProgressHUDDelegate>
{
    MBProgressHUD *hud;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btn1Pressed:(id)sender
{
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    
    [self.view addSubview:hud];
    hud.dimBackground = YES;
    hud.labelText = @"请稍后";
    [hud showAnimated:YES whileExecutingBlock:^{
        sleep(3);
    }completionBlock:^{
        [hud removeFromSuperview];
        hud = nil;
    }];
}

//MBProgressHUDModeIndeterminate,
///** Progress is shown using a round, pie-chart like, progress view. */
//MBProgressHUDModeDeterminate,
///** Progress is shown using a horizontal progress bar */
//MBProgressHUDModeDeterminateHorizontalBar,
///** Progress is shown using a ring-shaped progress view. */
//MBProgressHUDModeAnnularDeterminate,
///** Shows a custom view */
//MBProgressHUDModeCustomView,
///** Shows only labels */
//MBProgressHUDModeText
- (IBAction)btn2Pressed:(id)sender
{
    hud = hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.dimBackground = YES;
    hud.mode = MBProgressHUDModeCustomView;
    
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
    hud.labelText = @"请稍后";
    hud.delegate = self;
    
    [hud showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
}

- (void)myTask
{
    float progress = 0.0f;
    while (progress < 1.0f)
    {
        progress += 0.01f;
        hud.progress = progress;
        usleep(50000);//0.05s
    }
}
- (IBAction)btn3Pressed:(id)sender
{
    hud = hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.dimBackground = YES;
    hud.mode = MBProgressHUDModeCustomView;
    
    hud.labelText = @"请稍后";
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"特拉法尔加·罗.jpg"]];
    
    [hud showAnimated:YES whileExecutingBlock:^{
        sleep(3);
    }completionBlock:^{
        [hud removeFromSuperview];
        hud = nil;
    }];
    
}

- (IBAction)btn4Pressed:(id)sender
{
    hud = hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.dimBackground = YES;
    hud.mode = MBProgressHUDModeText;
    hud.labelText = @"请稍后";
    hud.yOffset = 50.0f;
    hud.xOffset = 50.0f;
    
    [hud showAnimated:YES whileExecutingBlock:^{
        sleep(3);
    }completionBlock:^{
        [hud removeFromSuperview];
        hud = nil;
    }];
    
}
- (IBAction)btn5Pressed:(id)sender {
}
- (void)hudWasHidden:(MBProgressHUD *)hud
{
    [hud removeFromSuperview];
    hud = nil;
}
@end
